<template>
  <Container>
    <div slot="tip" class="page-tip-div" style="margin-top: 0">
      <i class="el-icon-info"></i>
      <span>
        温馨提示：单位选择使用后，若编辑或者删除单位名称，会造成商品单位有误，请谨慎操作！！
      </span>
    </div>
    <div slot="left">
      <el-button
        v-if="$accessCheck($Access.UnitSetAdd)"
        size="small"
        type="primary"
        @click="openModel(false)"
      >
        新增单位
      </el-button>
    </div>
    <el-table :data="type_data">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column prop="unitName" label="名称"></el-table-column>
      <el-table-column prop="enableStatus" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-if="$accessCheck($Access.UnitSetUpdateEnablesStatus)"
            v-model="scope.row.enableStatus"
            class="switchStyle"
            active-color="#36B365"
            :active-value="5"
            :inactive-value="4"
            active-text="启用"
            inactive-text="禁用"
            @change="statusSet($event, scope.row)"
          ></el-switch>
          <div v-else>
            <span v-if="scope.row.enableStatus === 5" class="success-status">
              启用
            </span>
            <span v-else class="danger-status">禁用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="$accessCheck($Access.UnitSetEdit)"
            type="text"
            @click="openModel(true, scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="$accessCheck($Access.UnitSetDel)"
            type="text"
            @click="delData(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
    <el-dialog
      :title="(is_edit ? '编辑' : '新增') + '计量单位'"
      :visible.sync="show_model"
      width="40%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      @close="show_model = false"
    >
      <el-form
        ref="form"
        :model="add_form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="名称:" prop="unitName">
          <el-input
            v-model="add_form.unitName"
            placeholder="请输入单位名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否禁用:" prop="enableStatus">
          <el-radio-group v-model="add_form.enableStatus">
            <el-radio :label="4">是</el-radio>
            <el-radio :label="5">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show_model = false">取 消</el-button>
        <el-button type="primary" @click="addData">确 定</el-button>
      </span>
    </el-dialog>
  </Container>
</template>

<script>
  import {
    getAllUnit,
    addUnit,
    editUnit,
    delUnit,
    updateEnablesStatus,
    updateEnableStatus,
  } from "@/api/goods";
  export default {
    name: "UnitMeasurement",
    data() {
      return {
        pageSize: 10,
        page: 1,
        total: 0,
        show_model: false,
        is_edit: false,
        unit_id: 0,
        add_form: {
          unitName: "",
          enableStatus: 5,
        },
        rules: {
          unitName: [{ required: true, message: "请输入计量单位名称" }],
        },
        type_data: [],
      };
    },
    created() {
      this.getAllUnit();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getAllUnit();
    },
    methods: {
      // 获取列表
      async getAllUnit() {
        const { data, pageTotal } = await getAllUnit({
          page: this.page,
          pageSize: this.pageSize,
        });

        this.type_data = data;
        this.total = pageTotal;
      },

      async addData() {
        if (!this.add_form.unitName.trim()) {
          this.$message.warning("名称不能为空");
          return;
        }
        this.$refs["form"].validate(async (valid) => {
          if (valid) {
            let target = {};
            if (!this.is_edit) {
              target = await addUnit({
                ...this.add_form,
              });
            } else {
              target = await editUnit(this.unit_id, {
                ...this.add_form,
              });
            }
            this.show_model = false;
            this.pageChange(1);
          }
        });
      },
      sizeChange(size) {
        this.pageSize = size;
        this.pageChange(1);
      },
      pageChange(page) {
        this.page = page;
        this.getAllUnit();
      },
      openModel(isEdit, row) {
        this.add_form = {
          unitName: "",
          enableStatus: 5,
        };
        this.show_model = true;
        this.is_edit = isEdit;
        if (row) {
          this.unit_id = row.id;
          this.add_form = {
            unitName: row.unitName,
            enableStatus: row.enableStatus,
          };
        }
      },
      delData(id) {
        this.$confirm("确定要删除该计量单位吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await delUnit(id);

          this.getAllUnit();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      },
      async statusSet(val, row) {
        try {
          const data = await updateEnablesStatus({
            id: row.id,
            enableStatus: val,
          });
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        } catch (e) {
          this.getAllUnit();
        }
      },
    },
  };
</script>

<style scoped></style>
