<template>
  <div>
    <xy-dialog ref="dialog" :is-show.sync="isShow" type="form" :title="type === 'add' ? '新增访客区域' : '编辑访客区域'" :form="form"
      :rules="rules" @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>访客区域名称：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.name" placeholder="请输入访客区域名称" clearable style="width: 300px;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:audit_admin>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>审核流程：
          </div>
          <div class="xy-table-item-content">
            <Button
              type="primary"
              icon="md-add"
              style="margin-bottom: 10px"
              @click="addRow"
              >添加审核人节点</Button
            >

            <xy-table
              style="width: 620px"
              :height="260"
              :is-page="false"
              :list="form.audit_admin"
              :table-item="contentTable"
            >
              <template v-slot:btns>
                <el-table-column
                  label="操作"
                  width="90"
                  header-align="center"
                  align="center"
                >
                  <template v-slot:default="scope">
                    <Button
                      size="small"
                      type="primary"
                      ghost
                      @click="delRow(scope.$index)"
                      >删除</Button
                    >
                  </template>
                </el-table-column>
              </template>
            </xy-table>
          </div>
        </div>
      </template>

      <template v-slot:remark>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            备注：
          </div>
          <div class="xy-table-item-content">
            <el-input :autosize="{minRows:2}" type="textarea" v-model="form.remark" placeholder="请输入备注" clearable
              style="width: 300px;"></el-input>
          </div>
        </div>
      </template>


    </xy-dialog>


  </div>
</template>

<script>
  import {
    store,
    show,
    save
  } from "@/api/parameter/visitor.js"
  import {listCommonuser} from "@/api/common.js"
  export default {
    components: {},
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',

        form: {
          name:"",
          audit_admin:[],
          remark:""
        },
        checkList:[],
        rules:{
          name:[
            {
              required:true,
              message:'请输入区域名称'
            }
          ]
        },
        // 审核流程
        chenckTypes:[
          {
            id:0,
            value:'上级'
          },
          {
            id:1,
            value:'指定审核人'
          }
        ],
        checkAdmin:[],
        contentTable: [
          {
            label:"审核名称",
            customFn: (row,scope) => {
              return(
                this.checkList[scope.$index]
              )
            }
          },
          {
            label: "审核类别",
            width: 180,
            customFn: (row,scope) => {
              return (
                <el-select
                  placeholder="请选择审核类别"
                  style="width: 140px"
                  value={row.type_name}
                  on={{
                    ["change"]: (e) => (row.type_name=e.value,row.type = e.id,row.level = scope.$index+1,row.admin_id=row.type==0?0:row.admin_id,row.admin_name=''),
                  }}
                >
                {
                  this.chenckTypes.map((item,index)=>{
                    return(
                      <el-option
                        value={item}
                        key={item.id}
                        label={item.value}
                      >
                      </el-option>
                    )

                  })
                }
                </el-select>
              );
            },
          },{
            label: "审核人",
            width: 180,
            customFn: (row) => {
              if(row.type==0){
                return
              }
              return (
                <el-select
                  placeholder="请选择审核人"
                  style="width: 140px"
                  value={row.admin_name}
                  filterable
                  on={{
                    ["change"]: (e) => (row.admin_name = e.name,row.admin_id = e.id),
                  }}
                >
                {
                  this.checkAdmin.map((item,index)=>{
                    return(
                      <el-option
                        value={item}
                        key={item.id}
                        label={item.name}
                      >
                      </el-option>
                    )

                  })
                }
                </el-select>
              );
            },
          }]
      }
    },
    created() {
      this.getUser()
    },
    methods: {

      async getUser(){
        const res = await listCommonuser({page:1,page_size:999,show_all:1})
        this.checkAdmin = res.data
      },
      async getDetail() {
        const res = await show({
          id: this.id
        })
        this.form = {
          name:res?.name,
          audit_admin:res?.audit_admin,
          remark:res?.remark
        }
        let arr = []
        if(res.audit_admin.length>0){
          res.audit_admin.map((item,index)=>{
            arr.push(`${this.indexToChinese(index)}级审核人`)
          })
          this.checkList = arr
        }
      },
      addRow(){
        // if(this.form.audit_admin.length==3){
        //   return
        // }

         this.form.audit_admin.push({ type:'',admin_id:'',level:'',type_name:'',admin_name:'' })
         let checkName = `${this.indexToChinese(this.form.audit_admin.length-1)}级审核人`
         this.checkList.push(checkName)
         console.log("this.checkList",this.checkList)
      },
      delRow(index){
        this.form.audit_admin.splice(index, 1)
        this.checkList.splice(index, 1)
        let arr = []
        this.checkList.map((item,index)=>{
          arr.push(`${this.indexToChinese(index)}级审核人`)
        })
        this.checkList = arr
        console.log("this.arr",this.checkList)
      },
      indexToChinese(index) {
          const chineseNumbers = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
          if (index >= 0 && index < 10) {
              return chineseNumbers[index];
          } else if (index === 10) {
              return '十';
          } else if (index < 20) {
              return '十' + chineseNumbers[index - 11];
          } else {
              const tens = Math.floor(index / 10);
              const units = index % 10;
              let result = '';
              if (tens > 1) {
                  result += chineseNumbers[tens - 1] + '十';
              } else {
                  result += '十';
              }
              if (units > 0) {
                  result += chineseNumbers[units - 1];
              }
              return result;
          }
      },
      submit() {

        if(this.form.audit_admin.length>0){
          this.form.audit_admin.map((item,index)=>{
            item.level = index+1
          })
        }
        console.log(this.form)
        // return
        let that = this
        if (this.type === 'editor') {
          this.form.id = this.id
        }else{
          this.form.id = ""
        }
        save({
          ...that.form
        }).then(res => {
          this.$successMessage(this.type, '访客区域')
          this.isShow = false
          this.$emit('refresh')
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.type = ''
          this.checkList = []
          this.$refs['dialog'].reset()
        }
      }
    }

  }
</script>

<style scoped lang="scss">
  .xy-table-item-label {
    width: 160px;
  }

  .img__delete {
    transform: scale(0.8, 0.8);

    position: absolute;
    top: 4px;
    right: 4px;
  }
</style>
