<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      @submit.native.prevent
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="账号"
        width="150"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="昵称"
        width="150"
        align="center"
        prop="nickName"
      />
      <el-table-column
        label="职责"
        width="100"
        align="center"
        prop="postName"
      />
      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="更新人"
        align="center"
        prop="updateBy"
        width="100"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="150"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleView(scope.row, '查看用户权限')"
            v-hasPermi="['mes:qc:duty:view']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row, '修改用户权限')"
            v-hasPermi="['mes:qc:duty:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 查看或修改用户详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        label-position="right"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户账号" prop="userName">
              <el-input v-model="form.userName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="管理员" prop="qcIdentity">
              <el-radio-group
                v-model="form.qcIdentity"
                :disabled="title !== '修改用户权限'"
                @input="handleRadioChange"
              >
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="center" class="userdivider"
        >用户权限分配</el-divider
      >
      <div>
        <el-card shadow="always" class="usercard">
          <div slot="header">
            <span>权限分配</span>
            <el-button
              v-if="title === '修改用户权限'"
              style="float: right; padding: 3px 0"
              type="text"
              @click="handleAdd"
              >新增</el-button
            >
          </div>
          <el-table :data="outerTableData" style="width: 100%">
            <el-table-column prop="workstationCode" label="工作中心编码" />
            <el-table-column prop="workstationName" label="工作中心名称" />
            <el-table-column prop="processName" label="所属工序" />
            <el-table-column label="操作" v-if="title === '修改用户权限'">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </div>
      <el-dialog
        width="80%"
        title="工作中心"
        :visible.sync="visible"
        v-loading="innerLoading"
        append-to-body
      >
        <el-form
          :model="queryInnerParams"
          @submit.native.prevent
          ref="queryInnerForm"
          size="small"
          :inline="true"
        >
          <el-form-item prop="workstationCode">
            <el-input
              v-model="queryInnerParams.workstationCode"
              placeholder="请输入工作中心编码"
              clearable
              @keyup.enter.native="handleInnerQuery"
            />
          </el-form-item>
          <el-form-item prop="workstationName">
            <el-input
              v-model="queryInnerParams.workstationName"
              placeholder="请输入工作中心名称"
              clearable
              @keyup.enter.native="handleInnerQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleInnerQuery"
              >搜索</el-button
            >
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetInnerQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-table
          :data="innerTableData"
          style="width: 100%"
          highlight-current-row
          @current-change="handleCurrentChange"
        >
          <el-table-column prop="workstationCode" label="工作中心编码" />
          <el-table-column prop="workstationName" label="工作中心名称" />
          <el-table-column prop="processName" label="所属工序" />
        </el-table>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getListApi,
  getWorkListApi,
  getDetailApi,
  changeManagerApi,
  addWorkAuthApi,
  deleteWorkAuthApi,
} from "@/api/mes/qc/duty";
import { genCode } from "@/api/system/autocode/rule";
export default {
  name: "Duty",
  dicts: [
    "mes_shift_method",
    "mes_shift_type",
    "mes_calendar_type",
    "mes_order_status",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      innerLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      outerTableData: [],
      innerTableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
      },
      queryInnerParams: {
        pageNum: 1,
        pageSize: 10,
        workstationCode: null,
        workstationName: null,
      },
      // 表单参数
      form: {
        userName: null,
        nickName: null,
        qcIdentity: 0,
      },
      // 表单校验
      rules: {
        planCode: [
          { required: true, message: "计划编号不能为空", trigger: "blur" },
        ],
        planName: [
          { required: true, message: "计划名称不能为空", trigger: "blur" },
        ],
        calendarType: [
          { required: true, message: "请选择班组类型", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getListApi(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWorkList() {
      this.innerLoading = true;
      getWorkListApi(this.queryInnerParams).then((response) => {
        this.innerTableData = response.rows;
        this.innerLoading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        userId: null,
        userName: null,
        nickName: "",
        createBy: "",
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleInnerQuery() {
      this.queryParams.pageNum = 1;
      this.getWorkList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetInnerQuery() {
      this.resetForm("queryInnerForm");
      this.handleInnerQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.planId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 查询明细按钮操作
    handleView(row, title) {
      this.reset();
      const params = {
        userId: row.userId || this.ids,
      };
      getDetailApi(params).then((response) => {
        this.form = row;
        this.outerTableData = response.rows;
        this.open = true;
        this.title = title;
      });
    },
    /** 管理员权限更改 */
    handleRadioChange() {
      const params = {
        userId: this.form.userId,
        qcIdentity: this.form.qcIdentity,
      };
      changeManagerApi(params).then((response) => {
        if (response.code == 200) {
          return this.$message.success("切换管理员权限成功");
        }
      });
    },
    /** 新增按钮 */
    handleAdd() {
      this.visible = true;
      this.getWorkList();
    },
    /** 删除按钮 */
    handleDelete(row, index) {
      deleteWorkAuthApi(row.id).then((response) => {
        if (response.code == 200) {
          getDetailApi({
            userId: this.form.userId,
          }).then((response) => {
            this.outerTableData = response.rows;
          });
          return this.$message.success("删除成功");
        }
      });
    },
    handleCurrentChange(val) {
      if (val) {
        const params = {
          workstationId: val.workstationId,
          workstationCode: val.workstationCode,
          workstationName: val.workstationName,
          userId: this.form.userId,
          userName: this.form.userName,
          nickName: this.form.nickName,
        };
        addWorkAuthApi(params).then((response) => {
          if (response.code == 200) {
            this.visible = false;
            getDetailApi({
              userId: this.form.userId,
            }).then((response) => {
              this.outerTableData = response.rows;
            });
            return this.$message.success("添加成功");
          }
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.getList();
    },
  },
};
</script>
