<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model:page="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @row-del="rowDel"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button @click="handleAdd" type="primary">新增</el-button>
        <el-button type="danger" @click="handleDeletes">删 除</el-button>

        <el-button type="success" @click="handleUser">人员设置</el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" @click="handleImport">导入</el-button>
      </template>
      <template #menu="scope">
        <el-button type="text" @click="handleView(scope.row, 'view')">详情</el-button>
        <el-button type="text" @click="handleEdit(scope.row, 'add')">编辑</el-button>
        <el-button type="text" @click="handleDelete(scope.row, 'add')">删除</el-button>
        <el-button type="text" @click="handleIssue(scope.row, 'add')">下发</el-button>
      </template>
      <template #craftMan="{ row }">
        {{ row.craftMan.userName }}
      </template>
      <template #mecMan="{ row }">
        {{ row.mecMan.userName }}
      </template>
    </avue-crud>
    <!-- 新增 编辑 详情 -->
    <addProjectCycleDialog :title="title" v-if="showDialog" :showDialog="showDialog" @closeDialog="closeDialog"></addProjectCycleDialog>
    <!-- 人员设置 -->
    <el-dialog append-to-body :title="title1" v-model="issueDialog" width="550px">
      <el-form ref="issueForm" :model="issueForm" label-width="80px" :rules="formRules">
        <el-form-item label="工艺员" prop="userId">
          <el-select v-model="issueForm.userId">
            <el-option
              v-for="item in userData"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="issueDialog = false">取 消</el-button>
          <el-button type="primary" @click="submitIssue">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, remove } from '@/api/qualityManagement/periodicTesting/projectCycle.js';
import addProjectCycleDialog from './components/addProjectCycleDialog.vue'
export default {
  components:{
    addProjectCycleDialog
  },
  data() {
    return {
      loading: false,
      data: [],
      projectForm: {},
      projectRules: {
        tpProject: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        testPieceTitle: [{ required: true, message: '请选择试验件', trigger: 'blur' }],
        tpStandard: [{ required: true, message: '请输入试验标准', trigger: 'blur' }],
        craftMan: [{ required: true, message: '请选择工艺员', trigger: 'blur' }],
        remDays: [{ required: true, message: '请选择超期提醒', trigger: 'blur' }],
        textCycle: [{ required: true, message: '请选择试验周期', trigger: 'blur' }],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      issueDialog: false,
      issueForm: {},
      formRules: {
        userId: [{ required: true, message: '请选择工艺员', trigger: 'blur' }],
      },
      title1: '任务下发',
      userData: [
        {
          jobName: '软件工程师',
          deptName: 'MES开发部',
          ldapName: null,
          userSex: 1,
          deptId: 1,
          mobile: null,
          userName: '测试',
          userId: 521,
          userCode: '0001',
        },
        {
          jobName: null,
          deptName: 'MES开发部',
          ldapName: 'jinna',
          userSex: 2,
          deptId: 1,
          mobile: null,
          userName: '靳娜',
          userId: 21,
          userCode: 'jinna',
        },
        {
          jobName: null,
          deptName: 'MES开发部',
          ldapName: 'zd001',
          userSex: 1,
          deptId: 1,
          mobile: null,
          userName: '张迪',
          userId: 601,
          userCode: 'zd001',
        },
      ],
      selectionList: [],
      title: '新增',
      showDialog: false,
      monthTags: [],
      yearTags: [],
      option: {
        tip: false,
        height: 'auto',
        calcHeight: 32,
        columnSort: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: false,
        editBtnText: '修改',
        viewBtnText: '详情',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        labelWidth: 120,
        // menuWidth: 330,
        dialogWidth: 600,
        dialogClickModal: false,
        searchEnter: true,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        align: 'center',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        column: [
          {
            label: '镀种',
            prop: 'duzhong',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验件',
            prop: 'testPieceTitle',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验项目',
            prop: 'name',
            search: true,
            type: 'select',
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验条件',
            prop: 'tpCondition',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验标准',
            prop: 'tpStandard',
            search: false,
            sortable: true,
            width: 200,
          },

          {
            label: '工艺员',
            prop: 'craftMan',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验周期',
            prop: 'textCycle1',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '提前下发天数',
            prop: 'genBefore',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '超期提醒天数',
            prop: 'remind',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '更新时间',
            prop: 'updateTime',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    closeDialog(){
      this.showDialog = false;
    },
    // 点击详情按钮
    handleView(row) {
      this.projectForm = row;
      this.title = '详情';
      this.showDialog = true;
    },
    // 点击编辑按钮
    handleEdit(row) {
      this.projectForm = row;
      this.title = '编辑';
      if (this.projectForm.textCycle == '3') {
        this.yearTags = this.projectForm.tags;
      } else if (this.projectForm.textCycle == '2') {
        this.monthTags = this.projectForm.tags;
      } else {
        this.projectForm.weekList = this.projectForm.tags;
      }
      this.showDialog = true;
    },
    // 点击下发按钮
    handleIssue(row) {
      this.title1 = '任务下发';
      this.issueDialog = true;
    },
    // 点击上方删除按钮，多条删除
    handleDeletes() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据!');
      } else {
        this.$confirm('确定删除所选数据?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {});
      }
    },
    // 点击导入按钮
    handleImport() {},
    // 点击人员配置按钮
    handleUser() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据!');
      } else {
        this.title1 = '人员设置';
        this.issueDialog = true;
      }
    },
    // 点击删除按钮
    handleDelete(row) {
      this.$confirm('确定删除此条数据?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {});
    },
    // 点击新增按钮
    handleAdd() {
      this.title = '新增';
      this.showDialog = true;
      this.projectForm = {};
      this.monthTags = [];
      this.yearTags = [];
      this.$refs.form.resetFields();
    },
    // 切换周期
    changeCycle() {
      this.projectForm.monthValue = '';
      this.projectForm.weekList = [];
      this.monthTags = [];
      this.projectForm.yearMonth = '';
      this.projectForm.yearDays = '';
      this.yearTags = [];
    },
    // 添加月份日期
    addMonth() {
      let value = this.projectForm.monthValue + '日';
      if (this.monthTags.find(item => item == value)) return;
      this.monthTags.push(this.projectForm.monthValue + '日');
    },
    // 添加年份日期
    addYears() {
      let value = this.projectForm.yearMonth + '月' + this.projectForm.yearDays + '日';
      if (this.yearTags.find(item => item == value)) return;
      if (this.projectForm.yearMonth == '' || this.projectForm.yearDays == '') return;
      this.yearTags.push(this.projectForm.yearMonth + '月' + this.projectForm.yearDays + '日');
    },
    // 删除月份日期
    closeMonthTag(item, index) {
      this.monthTags = this.monthTags.filter(tags => tags != item);
    },
    // 删除年份日期
    closeYearsTag(item, index) {
      this.yearTags = this.yearTags.filter(tags => tags != item);
    },
    // 新增确定按钮
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.showDialog = false;
        }
      });
    },
    // 下发弹窗确定按钮
    submitIssue() {
      this.$refs.issueForm.validate(valid => {
        if (valid) {
          this.issueDialog = false;
        }
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;

      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.data = res.data.data.records;

        this.loading = false;
        this.page.total = this.data.length;
        // this.selectionClear();
      });
    },
  },
};
</script>

<style></style>
